
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <android/log.h>
#include "utils.h"
#include "jade.h"


jade_plane_t *jade_plane_create (int width, int height, float scale) {

    jade_plane_t *plane = NULL;
    int vt_idx = 0, nm_idx = 0, uv_idx = 0;
    float *vertices = NULL;
    float *normals = NULL;
    float *uvs = NULL;
    int x = 0, z = 0;

#ifdef  FLOAT_ARG_FAIL
    scale = 12.0f;
#endif

    LOGD ("jade_plane_create: width = %d, height = %d, scale = %f.\n", width , height, scale);

    plane = (jade_plane_t *)malloc (sizeof (jade_plane_t));
    if (plane == NULL)
        return NULL;

    plane->mesh = jade_mesh_create ();

    vertices = (float *) malloc(width * height * 18 * sizeof(float));
    normals = (float *) malloc(width * height * 18 * sizeof(float));
    uvs = (float *) malloc(width * height * 12 * sizeof(float));

    /* Todo check vertices, normals, usv */
    //...

    for (x = 0; x < width; x++) {
        for (z = 0; z < height; z++) {

            //triangle 1 first point 
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;


            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;
               
            uvs[uv_idx++] = 0.0f;
            uvs[uv_idx++] = 0.0f;

            //second point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;
                
            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;

            uvs[uv_idx++] = 1.0f;
            uvs[uv_idx++] = 0.0f;

            //third point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;

            uvs[uv_idx++] = 1.0f;
            uvs[uv_idx++] = 1.0f;


            //triangle 1 first point
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;

            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;

            uvs[uv_idx++] = 0.0f;
            uvs[uv_idx++] = 0.0f;

            //second point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;

            uvs[uv_idx++] = 1.0f;
            uvs[uv_idx++] = 1.0f;

            //third point
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

            normals[nm_idx++] = 0.0f;
            normals[nm_idx++] = 1.0f;
            normals[nm_idx++] = 0.0f;

            uvs[uv_idx++] = 0.0f;
            uvs[uv_idx++] = 1.0f;


        }
    }

    //set data to model
    jade_mesh_set_vertices (plane->mesh, vertices, width * height * 18 * sizeof(float));
    jade_mesh_set_normals (plane->mesh, normals, width * height * 18 * sizeof(float));
    jade_mesh_set_uvs (plane->mesh, uvs, width * height * 12 * sizeof(float));
    jade_mesh_set_triangle_num (plane->mesh, width * height * 2);

    FREEANDNULL(vertices);
    FREEANDNULL(uvs);
    FREEANDNULL(normals);
  

    return plane;

}

void jade_plane_destroy (jade_plane_t *plane) {

    if (plane->mesh != NULL)
        jade_mesh_destroy (plane->mesh);

    FREEANDNULL (plane);

    return;
}


void jade_plane_set_texture_id (jade_plane_t *plane, GLint textureId) {

    jade_mesh_set_texture_id (plane->mesh, textureId);

    return;
}

void jade_plane_set_position (jade_plane_t *plane, GLfloat x, GLfloat y, GLfloat z) {

    LOGD ("jade_plane_set_position: x = %f, y = %f, z = %f.\n", x, y, z);

#ifdef FLOAT_ARG_FAIL
    jade_mesh_set_position (plane->mesh, -10.0f, -2.0f, -3.0f);
#else
    jade_mesh_set_position (plane->mesh, x, y, z);
#endif    

    return;
}

void jade_plane_render (jade_plane_t *plane) {

    if (plane->mesh != NULL)
        jade_mesh_render (plane->mesh);


}

jade_plane_t *jade_plane_create_test () {

    int x = 0, z = 0;
    int width = 0, height = 0, i = 0;
    float scale = 0.0f;
    float *vertices = NULL;
    int vt_idx = 0;
    jade_plane_t *plane = NULL;

    plane = (jade_plane_t *)malloc (sizeof (jade_plane_t));
    if (plane == NULL)
        return NULL;

    plane->mesh = jade_mesh_create ();
/*
    //triangle data
    GLfloat vertices[9] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f, 1.0f, 0.0f
    };*/
    GLubyte colors[12] = {
        255, 0, 0, 0,
        0, 255, 0, 0,
        0, 0, 255 ,0
    };


    width = 4;
    height = 4;
    scale = 12.0f;

    vertices = (float *) malloc(width * height * 18 * sizeof(float));

    for (x = 0; x < width; x++) {
        for (z = 0; z < height; z++) {
            
            //triangle 1 first point 
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;

            //second point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;
                
            //third point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

            //triangle 1 first point
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = z * scale;

            //second point
            vertices[vt_idx++] = (x + 1) * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

            //third point
            vertices[vt_idx++] = x * scale;
            vertices[vt_idx++] = 0.0f;
            vertices[vt_idx++] = (z + 1) * scale;

        }
    }

    
        

/*
    //set data to model
    jade_mesh_set_vertices (plane->mesh, vertices, 9 * sizeof(float));
    jade_mesh_set_colors (plane->mesh, colors, 12 * sizeof(GLubyte));
    jade_mesh_set_triangle_num (plane->mesh, 1);
*/
    jade_mesh_set_vertices (plane->mesh, vertices, width * height * 18 * sizeof(float));
    jade_mesh_set_colors (plane->mesh, colors, 12 * sizeof(GLubyte));
    jade_mesh_set_triangle_num (plane->mesh, width * height * 2);

    FREEANDNULL(vertices);
 
    return plane;

}

